var ctx;

function setCtx(ctx) {
	this.ctx = ctx;
}

//验证楼栋名
function checkBuildingName(){
	var buildingName = $("#buildingName").val();
	//解决ie9以下placeholder与value重复的问题
	if(buildingName == $("#buildingName").attr("placeholder")){
		buildingName = "";
	}
	
	if(buildingName == null || buildingName == ""){
		layer.tips('楼栋名不能为空!', '#buildingName', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证区域名
function checkRegionName(){
	var regionName = $("#regionid").val();
	
	if(regionName == null || regionName == ""){
			layer.tips('请选择区域名!', '#regionid', {
				tips: 3,
			    time:1000
			});
		return false;
	}else{
		return true;
	}
}

//验证排序不能为空
function checkSort(){
	var reg = /^[0-9]*[1-9][0-9]*$/;
	var sort = $("#sort").val();
	if(sort == null || sort == ""){
		layer.tips('排序不能为空!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else if(!reg.test(sort)){
		layer.tips('请输入正整数!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//新增或编辑小区区域(1:新增 2:编辑)
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
function saveOrUpdateCellBuild(){
	var id;
	if($("#cellBuildingId").val() != null && $("#cellBuildingId").val() != ''){
		id = $("#cellBuildingId").val();
	}else{
		id = 0;
	}
	var buildingName = $("#buildingName").val();
	var memo = $("#memo").val();
	var sort = $("#sort").val();
	if(checkBuildingName() && checkRegionName() && checkSort()){
		var index1 = layer.load(1);
		var regionid = $("#regionid").val();
		$.post(ctx+"/cellBuilding/saveOrUpdateCellBuilding",
				{
			id:id,
			regionid:regionid,
			buildingName:buildingName,
			memo:memo,
			sort:sort
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("操作成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							parent.loadContent("/cellBuilding/cellBuildingList");
							parent.layer.close(index);
						});
					} else {
						layer.close(index1);
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
	}
}